package com.dbuil.bleshoes.utils;

import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.DrawableRes;
import androidx.core.content.FileProvider;

import com.blankj.utilcode.util.ToastUtils;

import java.io.File;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

public class ShareUtils {

    private Context context;

    public ShareUtils(Context context) {
        this.context = context;
    }

    public void shareText(String content,String packagename,String classsname) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);

        //如果传入的包名为空，则不指定应用,如果传入包名不为空，类名为空，则指定应用但是不指定页面
        if (!TextUtils.isEmpty(packagename)&&TextUtils.isEmpty(classsname)) {
            shareIntent.setPackage(packagename);
        }
        //如果传入包名不为空，类名也不为空，则指定分享跳转页面
        if (!TextUtils.isEmpty(packagename)&&!TextUtils.isEmpty(classsname)) {
            //指定分享到某个应用的页面
            ComponentName comp = new ComponentName(packagename, classsname);
            shareIntent.setComponent(comp);
        }
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, content);
        //切记需要使用Intent.createChooser，否则会出现别样的应用选择框，您可以试试
        shareIntent = Intent.createChooser(shareIntent, "分享到");
        startActivity(shareIntent);
    }

    public Uri U;

    public void shareImageAll(String path) {

        if (TextUtils.isEmpty(path)) {
            ToastUtils.showLong("图片不存在");
            return;
        }
        File file = new File(path);
        if (Build.VERSION.SDK_INT < 24) {
            U = Uri.fromFile(file);
        } else {
            //android 7.0及以上权限适配
            U = FileProvider.getUriForFile(context,
                    "com.dbuil.bleshoes.fileprovider",
                    file);
        }
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享图片subject");
        intent.putExtra(Intent.EXTRA_STREAM, U);


//        Intent receiver = new Intent(context, ShareDoneReceiver.class);
//        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
//       //切记需要使用Intent.createChooser，否则会出现别样的应用选择框，您可以试试
//        intent = Intent.createChooser(intent, "分享到", pendingIntent.getIntentSender());
//        startActivity(intent);


        intent = Intent.createChooser(intent, "分享到");
        startActivity(intent);

    }


    /**
     * 分享到指定应用
     *
     * @param path        图片路径
     * @param packagename 应用包名
     */
    public void shareImagePackage(String path, String packagename) {

        File file = new File(path);
        if (Build.VERSION.SDK_INT < 24) {
            U = Uri.fromFile(file);
        } else {
            //android 7.0及以上权限适配
            U = FileProvider.getUriForFile(context,
                    "com.dbuil.bleshoes.fileprovider",
                    file);
        }
        Intent intent = new Intent(Intent.ACTION_SEND);
        // 指定分享到某个应用,QQ com.tencent.mobileqq
        intent.setPackage(packagename);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享到");
        intent.putExtra(Intent.EXTRA_STREAM, U);

        intent = Intent.createChooser(intent, "分享到");
        startActivity(intent);

    }


    /**
     * 分享到指定应用的指定页面
     *
     * @param path
     * @param packagename 例如微信 com.tencent.mm
     * @param classname   例如微信好友  com.tencent.mm.ui.tools.ShareImgUI  QQ好友 com.tencent.mobileqq.activity.JumpActivity  微博 com.sina.weibo.activity.JumpActivity
     */
    public void shareImageClassname(String path, String packagename, String classname) {

        File file = new File(path);
        if (Build.VERSION.SDK_INT < 24) {
            U = Uri.fromFile(file);
        } else {
            //android 7.0及以上权限适配
            U = FileProvider.getUriForFile(context,
                    "com.dbuil.bleshoes.fileprovider",
                    file);
        }
        Intent intent = new Intent(Intent.ACTION_SEND);

        // "com.tencent.mm.ui.tools.ShareImgUI"发送给微信联系人
        //"com.tencent.mm","com.tencent.mm.ui.tools.AddFavoriteUI"添加到收藏
        //"com.tencent.mm.ui.tools.ShareToTimeLineUI" 发送到朋友圈

        //指定分享到某个应用的页面
        ComponentName comp = new ComponentName(packagename, classname);
        intent.setComponent(comp);

        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享到");
        intent.putExtra(Intent.EXTRA_STREAM, U);
        intent.putExtra(Intent.EXTRA_TEXT, "分享的文字内容");
        intent = Intent.createChooser(intent, "分享到");
        startActivity(intent);

    }


    /**
     * 资源文件转URI   Uri uri1 = Uri.parse(getResourcesUri(R.drawable.dog));
     *
     * @param id
     * @return
     */
    private String getResourcesUri(@DrawableRes int id) {
        Resources resources = context.getResources();
        String uriPath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                resources.getResourcePackageName(id) + "/" +
                resources.getResourceTypeName(id) + "/" +
                resources.getResourceEntryName(id);
        // Toast.makeText(context, "Uri:" + uriPath, Toast.LENGTH_SHORT).show();
        return uriPath;
    }


}
